﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Runtime.Extensions;

namespace PServiceBus.Core.Manager {
    public class SubscriberManager {
        public static void Invoke(Action<IObjectProvider<ISubscriber>> action) {
            var providers = SubscriptionManagerManager.Managers.Select(x => x.Provider);
            foreach (var provider in providers)
                action(provider);
        }

        private static FuncEqualityComparer<ISubscriber> _comparer =
            new FuncEqualityComparer<ISubscriber>((s1, s2) => s1.Name == s2.Name, x => x.Name.GetHashCodeEx());

        public static IEnumerable<ISubscriber> Current {
            get {
                  return SubscriptionManagerManager.Managers.SelectMany(x => x.Provider)
                        .Distinct(_comparer);
            }
        }
    }
}
